package com.dsdl.chatapp.common.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.dsdl.chatapp.data.source.local.ChatPersistenceContract;

/**
 * Created by zzg on 2016/8/1.
 * SQLite辅助类
 */
public class DBHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "chat.db";
    private static final int DATABASE_VERSION = 1;

    private static final String TEXT_TYPE = " TEXT";

    private static final String INTEGER_TYPE = " INTEGER";

    private static final String VARCHAR_TYPE = " VARCHAR";

    private static final String COMMA_SEP = ",";

    private static final String SQL_CREATE_CHAT =
            "CREATE TABLE "+ ChatPersistenceContract.ChatEntry.TABLE_NAME+" ("+
                    ChatPersistenceContract.ChatEntry._ID+INTEGER_TYPE+" PRIMARY KEY AUTOINCREMENT NOT NULL,"+
                    ChatPersistenceContract.ChatEntry.COLUMN_NAME_MSG_ID+VARCHAR_TYPE+COMMA_SEP+
                    ChatPersistenceContract.ChatEntry.COLUMN_NAME_FROM_USER+VARCHAR_TYPE+COMMA_SEP+
                    ChatPersistenceContract.ChatEntry.COLUMN_NAME_TO_USER+VARCHAR_TYPE+COMMA_SEP+
                    ChatPersistenceContract.ChatEntry.COLUMN_NAME_MSG_TYPE+VARCHAR_TYPE+COMMA_SEP+//system(系统消息),text（文本消息）,event（事件类消息，如添加好友）,image（图片消息）.voice（语音消息）,video（视频消息）,location（位置消息）
                    ChatPersistenceContract.ChatEntry.COLUMN_NAME_CONTENT+TEXT_TYPE+COMMA_SEP+
                    ChatPersistenceContract.ChatEntry.COLUMN_NAME_CREATE_TIME+VARCHAR_TYPE+COMMA_SEP+
                    ChatPersistenceContract.ChatEntry.COLUMN_NAME_SEND_TIME+VARCHAR_TYPE+" )";
    private static final String SQL_CREATE_USER =
            "CREATE TABLE "+ChatPersistenceContract.UserEntry.TABLE_NAME+" ("+
                    ChatPersistenceContract.UserEntry._ID+INTEGER_TYPE+" PRIMARY KEY AUTOINCREMENT NOT NULL,"+
                    ChatPersistenceContract.UserEntry.COLUMN_NAME_USER_ID+VARCHAR_TYPE+COMMA_SEP+
                    ChatPersistenceContract.UserEntry.COLUMN_NAME_USER_ID+VARCHAR_TYPE+COMMA_SEP+
                    ChatPersistenceContract.UserEntry.COLUMN_NAME_USER_NICK+VARCHAR_TYPE+COMMA_SEP+
                    ChatPersistenceContract.UserEntry.COLUMN_NAME_USER_REMARK+VARCHAR_TYPE+COMMA_SEP+
                    ChatPersistenceContract.UserEntry.COLUMN_NAME_USER_PHONE+VARCHAR_TYPE+COMMA_SEP+
                    ChatPersistenceContract.UserEntry.COLUMN_NAME_USER_PHOTO+VARCHAR_TYPE+" )";

    //为MsgId字段创建索引，目前还不需要，看数据量大小
    private static final String SQL_CREATE_CHAT_INDEX = "CREATE INDEX MsgId_index ON "+ChatPersistenceContract.ChatEntry.TABLE_NAME+" ("+ChatPersistenceContract.ChatEntry.COLUMN_NAME_MSG_ID+")";


    public DBHelper(Context context){
        super(context,DATABASE_NAME,null,DATABASE_VERSION);
    }

    public DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL(SQL_CREATE_CHAT);
//        sqLiteDatabase.execSQL(SQL_CREATE_USER);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }

}
